function [newSeqString newSeq] = addBlanksAndTargets(baseSeq, SOA, numTargets, refreshBlockAfterBlanks, refreshBlockAfterTargets)

TRDuration = 3000;
min_number_of_trials_without_targets_in_the_beginning = 10;
always_have_one_target_within_this_many_trials_to_the_end = 12;


seqLength = length(baseSeq);
numSyncTrials = lcm(SOA, TRDuration) / SOA; % Number of trials required to sync scanner-experiment

%numTrials = (base sequence length) + (targets and refresh after targets) +
% + (2 blank-blocks and refresh after blanks) + 
% + (the last blank-block that doesn't require refresh)
numTrials = seqLength + numTargets*(refreshBlockAfterTargets+1) + 3*numSyncTrials;

if mod(numTrials,numSyncTrials) ~= 0
    error('Final sequence length does not allow proper syncing. Check number of targets and refresh-block lengths.');
end


%% Create a string to design the sequence

newSeqString = blanks(numTrials);
newSeqString(newSeqString==' ') = '-';
newSeqString = reshape(newSeqString,numSyncTrials,numTrials/numSyncTrials);


%% Add blank blocks

blankBlock = [];
for i=1:(numSyncTrials-refreshBlockAfterBlanks)
    blankBlock = [blankBlock; 'B'];
end

refreshBlock = [];
for i=1:refreshBlockAfterBlanks
    refreshBlock = [refreshBlock; 'R'];
end

newSeqString(:,1) = [blankBlock; refreshBlock];

newSeqString(:,ceil(median(1:(numTrials/numSyncTrials)))) = [blankBlock; refreshBlock];

refreshBlock(refreshBlock == 'R') = 'B';
newSeqString(:,end) = [blankBlock; refreshBlock];


%% Add target trials

candidateTrials = find(newSeqString=='-');

% Cut out the first few trials
candidateTrials = candidateTrials(min_number_of_trials_without_targets_in_the_beginning:end);
% Also, remove the very last trials to allow for the refresh block to be
% included after the last target
candidateTrials = candidateTrials(1:(end-(refreshBlockAfterTargets+3)));
% Finally, remove the few trials right before the middle blank block, to
% allow for the refresh block to be included after a possible target
candidateTrials = candidateTrials(~ismember(candidateTrials,(((ceil(median(1:(numTrials/numSyncTrials)) - 1))*numSyncTrials - (refreshBlockAfterTargets+2)):((ceil(median(1:(numTrials/numSyncTrials)) - 1))*numSyncTrials))));

% Now, pick 4 that are spaced apart at least by 10 elements, and that is
% not more than a few elements before the end (specified as a parameter)
targetPositions = ones(1,numTargets);
while (min(targetPositions(2:end)-targetPositions(1:end-1)) < 10) || (max(targetPositions) < ((size(newSeqString,2)-1)*numSyncTrials+1 - always_have_one_target_within_this_many_trials_to_the_end))
    % Pick numTargets arbitrary numbers from candidateTrials
    targetPositions = candidateTrials(sort(ceil(rand(1,numTargets)*length(candidateTrials))));
end

targetBlock = 'T';
for i=1:refreshBlockAfterTargets
    targetBlock = [targetBlock 'R'];
end

for i=1:length(targetPositions)
    newSeqString(targetPositions(i):(targetPositions(i)+refreshBlockAfterTargets)) = targetBlock;
end



%% Now, create the real sequence
newSeq = nan(size(newSeqString));

trialIndex = 1;

% Add valid trials
for i=1:length(newSeq(:))
    if newSeqString(i) == '-'
        newSeq(i) = baseSeq(trialIndex);
        trialIndex = trialIndex + 1;
    end
end

% Add overlap trials
copyingOverlapStarted = 0;
j = nan;
this_is_a_target_refresh_block = false;
this_is_a_blank_refresh_block = false;
for i=1:length(newSeq(:))
    if ((newSeqString(i) == 'R') && (copyingOverlapStarted == 0))
        copyingOverlapStarted = 1;
        j=i;
        % Determine what type of refresh block this is
        if newSeqString(i-1) == 'T'
            this_is_a_target_refresh_block = true;
        elseif newSeqString(i-1) == 'B'
            this_is_a_blank_refresh_block = true;
        end
        % Find the last valid trial and save as j
        while newSeqString(j) ~= '-'
            if j > 1
                j = j - 1;
            else
                j = length(newSeq(:));
            end 
        end
        if this_is_a_target_refresh_block
            j = j - (refreshBlockAfterTargets-1);
        elseif this_is_a_blank_refresh_block
            j = j - (refreshBlockAfterBlanks-1);
        end
        newSeq(i) = newSeq(j);
        j = j + 1;
    elseif ((newSeqString(i) == 'R') && (copyingOverlapStarted == 1))
        newSeq(i) = newSeq(j);
        j = j + 1;
    else
        copyingOverlapStarted = 0;
        j = nan;
        this_is_a_target_refresh_block = false;
        this_is_a_blank_refresh_block = false;
    end
end

for i=1:length(newSeq(:))
    switch newSeqString(i)
        case 'B'
            newSeq(i) = 0;
        case 'T'
            newSeq(i) = -1;
    end
end


